//
// Created by ASUS on 2021/11/16.
//

#include "handler.h"
#include <string.h>
#include <stdio.h>

char * command_list[COMMAND_NUM] = {
        "quit",
        "download",
        "chrome_pwd",
        "cd",
        "shot"
};

int get_cmd_opt(char * input, char * cmd, char ** opt) {
    char * tokens[OPT_NUM] = {NULL};
    int i = 0;
    strcpy(cmd, strtok(input, " "));
    while ((opt[i] = strtok(NULL, " ") )) {
        i ++;
        if (i >= OPT_NUM) {
            printf("too many arguments!\n");
            return -1;
        }
    }
    return i;
}

int handler(char * input, char * cmd, char ** opt) {

    get_cmd_opt(input, cmd, opt);
    int i;
    for (i = QUIT; i < COMMAND_NUM; i ++) {
        strlwr(cmd);
        if (strcmp(command_list[i], cmd) == 0) {
            return i;
        }
    }
    return -1;
}